package com.example.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.EntrustMapper;
import com.example.mapper.ProjectMapper;
import com.example.mapper.TeamMapper;
import com.example.pojo.entity.Entrust;
import com.example.pojo.entity.Project;
import com.example.pojo.entity.Team;
import com.example.pojo.vo.TreeData;
import com.example.service.EntrustService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author ding
 * @since 2024/6/6
 */
@Service
@DS("master")
public class EntrustServiceImpl extends ServiceImpl<EntrustMapper, Entrust> implements EntrustService {

    @Autowired
    private EntrustMapper entrustMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private TeamMapper teamMapper;

    /**
     * 根据委托单位名称，查询委托单位信息
     * @param entrustName   委托单位名称
     */
    public Entrust selectByEntrustName(String entrustName){
        QueryWrapper<Entrust> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(entrustName), "name", entrustName);
        return entrustMapper.selectOne(queryWrapper);
    }

    /**
     * 分页获取发布单位信息
     */
    public Page<Entrust> selectPage(String name, Integer pageNum, Integer pageSize) {
        return entrustMapper.selectPage(new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<Entrust>()
                        .eq(Entrust::getIsDeleted, 0)
                        .like(StringUtils.hasLength(name), Entrust::getName, name));
    }

    /**
     * 根据部门id获取该部门颁布的项目信息
     */
    public List<Project> selectProjectByEntrustId(Integer id) {
        return new ArrayList<Project>(projectMapper
                .selectList(new LambdaQueryWrapper<Project>()
                        .eq(Project::getEntrustId, id)));
    }

    public void updateEntrust(String name) {
        entrustMapper.update(new LambdaUpdateWrapper<Entrust>()
                .set(Entrust::getName, name));
    }

    //树状图数据
    public TreeData treeData(Integer id) {
        Entrust entrust = entrustMapper.selectOne(new LambdaQueryWrapper<Entrust>().eq(Entrust::getId, id));
        TreeData entData = new TreeData();
        entData.setName(entrust.getName());

        List<TreeData> entC = new ArrayList<>();

        List<Project> projectList = projectMapper.selectList(new LambdaQueryWrapper<Project>().eq(Project::getEntrustId, id));
        for (Project project : projectList) {
            TreeData proData = new TreeData();
            proData.setName(project.getName());
            if (ObjectUtil.isNotEmpty(project.getTeamId())) {
                List<TreeData> proC = new ArrayList<>();
                TreeData teamData = new TreeData();
                Team team = teamMapper.selectOne(new LambdaQueryWrapper<Team>().eq(Team::getId, project.getTeamId()));
                teamData.setName(team.getName());
                proC.add(teamData);
                proData.setChildren(proC);
            }
            entC.add(proData);
        }
        entData.setChildren(entC);

        return entData;
    }
}
